﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Microsoft.VisualStudio.Shell;
using EnvDTE;

namespace NickPowell.LiveCode
{
    public class Project
    {
        private User user;

        #region Project info variables
        private int iID = -1;
        private string sName = "";

        private string sFilePath = "";
        private List<string> lstRelativePaths = new List<string>();
        #endregion

        private int iTextSeq = 0;

        private DocumentEvents docEvents;

        private List<Document> lstDocuments = new List<Document>();

        public int TextSeq
        {
            get
            {
                return this.iTextSeq;
            }
            set
            {
                this.iTextSeq = value;
            }
        }
        public User User
        {
            get
            {
                return this.user;
            }
        }
        public List<Document> Documents = new List<Document>();

        public Project(int projectID, string projectName, string projectPath, List<string> relativeProjectPaths, User _user)
        {
            try
            {
                this.user = _user;

                this.iID = projectID;
                this.sName = projectName;

                this.sFilePath = projectPath;
                this.lstRelativePaths = relativeProjectPaths;

                this.StartDocListening();
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        private void StartDocListening()
        {
            DTE env = Package.GetGlobalService(typeof(Microsoft.VisualStudio.Shell.Interop.SDTE)) as DTE;
            if (env != null)
            {
                EnvDTE.Events events = env.Events;
                docEvents = events.get_DocumentEvents(null);
                docEvents.DocumentOpened += new _dispDocumentEvents_DocumentOpenedEventHandler(DocumentOpened);
                docEvents.DocumentClosing += new _dispDocumentEvents_DocumentClosingEventHandler(DocumentClosing);
            }
        }

        public void ProcessTextChange(XmlDocument xmlDoc)
        {
            string sRelativePath = xmlDoc.SelectSingleNode("/LiveCode/Text/@RelativePath").Value;

            //Make sure the document sent is part of this project
            if (lstRelativePaths.Contains(sRelativePath))
            {
                List<Document> lstDocs = this.lstDocuments.Where(d => d.RelativePath == sRelativePath).ToList();
                //Only change text if document has been opened
                if(lstDocs.Count() == 1)
                {
                    lstDocs[0].EditText(xmlDoc);
                }
            }
        }

        #region Document Events
        private void DocumentOpened(EnvDTE.Document _doc)
        {
            if (lstRelativePaths.Contains(_doc.FullName.Replace(sFilePath, "")))
            {
                this.lstDocuments.Add(new Document(_doc.FullName.Replace(sFilePath, ""), this));
            }
        }

        private void DocumentClosing(EnvDTE.Document _doc)
        {
            if (lstRelativePaths.Contains(_doc.FullName.Replace(sFilePath, "")))
            {
                //TODO remove listener
                //throw new NotImplementedException();
            }
        }
        #endregion
    }
}
